package com.example.bbs.config.redis;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class UserBasicInfoRedisConfig {


    @Value("${redis.post.host4}")
    private String host;

    @Value("${redis.post.port4}")
    private Integer port;

    @Value("${redis.post.database4}")
    private Integer database;

    @Bean("userBasicInfoRedisTemplate")
    public RedisTemplate<String, Object> userBasicInfoRedisTemplate(
            @Qualifier("userBasicInfoLettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        //配置序列化方式
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.json());
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.json());
        //配置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        //加载配置
        template.afterPropertiesSet();
        return template;
    }


    @Bean("userBasicInfoLettuceConnectionFactory")
    public LettuceConnectionFactory userBasicInfoLettuceConnectionFactory() {
        //配置连接信息
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setDatabase(database);
        return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }
}
